package com.example.blogapi.service.impl;


import com.alibaba.fastjson.JSON;
import com.example.blogapi.domain.MsSysUser;
import com.example.blogapi.service.LoginService;
import com.example.blogapi.service.MsSysUserService;
import com.example.blogapi.utils.JWTUtils;
import com.example.blogapi.vo.ErrorCode;
import com.example.blogapi.vo.Result;
import com.example.blogapi.vo.param.LoginParam;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
public class LoginServiceImpl implements LoginService {
//  加密盐
    private static final String salt = "ZwSalt%*^&!";
    @Autowired
    HashMap<String,String> fakeRedis;
    @Autowired
    MsSysUserService msSysUserService;

    @Override
    public Result login(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        if (StringUtils.isNullOrEmpty(account)||StringUtils.isNullOrEmpty(password)){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMes());
        }
        String md5Password= DigestUtils.md5Hex(password + salt);
        MsSysUser user = msSysUserService.findUser(account,md5Password);
        if (user == null){
            return Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMes());
        }
        String token = JWTUtils.createToken(user.getId());
//        这里可以使用redis存放数据，我暂时用一个hashmap存放
        fakeRedis.put("TOKEN_"+token,JSON.toJSONString(user));
        return Result.success(token);
    }


    /**
     * 登出方法
     * @param token
     * @return
     */
    @Override
    public Result logout(String token) {

        fakeRedis.remove("TOKEN_"+token);
        return Result.success(null);
    }

    /**
     * 注册方法
     * @param loginParam
     * @return
     */
    @Override
    @Transactional
    public Result register(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        String nickname = loginParam.getNickname();
        if (StringUtils.isNullOrEmpty(account)
                || StringUtils.isNullOrEmpty(password)
                || StringUtils.isNullOrEmpty(nickname)
        ){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMes());
        }
        MsSysUser msSysUser = this.msSysUserService.findUserByAccount(account);
        if (msSysUser != null){
            return Result.fail(ErrorCode.ACCOUNT_EXIST.getCode(),ErrorCode.ACCOUNT_EXIST.getMes());
        }
        MsSysUser user = new MsSysUser();
        user.setNickname(nickname);
        user.setAccount(account);
        user.setPassword(DigestUtils.md5Hex(password+salt));
        user.setCreateDate(System.currentTimeMillis());
        user.setLastLogin(System.currentTimeMillis());
        user.setAvatar("");
        user.setAdmin(true); //1 为true
        user.setDeleted(false); // 0 为false
        user.setSalt("");
        user.setStatus("");
        user.setEmail("");
        this.msSysUserService.save(user);

        String token = JWTUtils.createToken(user.getId());

        fakeRedis.put("TOKEN_"+token, JSON.toJSONString(user));
        return Result.success(token);
    }
}
